//
//  NSDictionary+GetMethod.m
//
//  Created by 张 桀宁 on 2016-11-3.
//  Copyright © 2016年 Alipay. All rights reserved.
//

#import "NSDictionary+GetMethod.h"
#define EmptyString(aString) (nil == aString || 0 == [aString length])
#define NotEmptyString(aString) (nil != aString && 0 < [aString length])

#define EmptyDicOrArray(aobj) (nil == aobj || 0 == [aobj count])
#define NotEmptyDicOrArray(aobj) (nil != aobj && 0 < [aobj count])

#define NotNilString(aString) (aString ?: @"")

NSString* stringTrimed(NSString* str4Trim) {
    if(EmptyString(str4Trim)) {
        return @"";
    }
    
    return [str4Trim stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

@implementation NSDictionary (GetMethod)

// 如果dic中没有有效值, 返回nil
- (id) validValue:(id)thekey expectType:(Class)vtype {
    id rtnvalue = [self objectForKey:thekey];
    if(nil == rtnvalue){
        return nil;
    }
    if([[NSNull null] isEqual:rtnvalue]){
        return nil;
    }
    if([rtnvalue isKindOfClass:vtype]){
        return rtnvalue;
    } else {
        return nil;
    }
}

// 如果dic中没有有效值, 返回""
- (NSString*) validStringValue:(id)thekey {
    return [self validStringValue:thekey withDefault:@""];
}

// 如果dic中没有有效值, 且defaultStr有效，返回defaultStr,否则返回""
- (NSString*) validStringValue:(id)thekey withDefault:(NSString*)defaultStr {
    NSString* thevalue = [self validValue:thekey expectType:[NSString class]];
    if(nil == thevalue){
        NSNumber* thenum = [self validValue:thekey expectType:[NSNumber class]];
        if(nil == thenum) {
            if(nil != defaultStr) {
                return defaultStr;
            }
            return @"";
        } else {
            return [NSString stringWithFormat:@"%@",thenum];
        }
    }
    if ([@"null" isEqualToString:thevalue] || [@"" isEqualToString:thevalue]) {
        if(nil != defaultStr) {
            return defaultStr;
        }
        return @"";
    }
    return thevalue;
}

// 如果dic中没有有效值, 返回0.f
- (float) validFloatValue:(id)thekey {
    return [self validFloatValue:thekey withDefault:0.f];
}

// 如果dic中没有有效值, 返回defaultFloat
- (float) validFloatValue:(id)thekey withDefault:(float)defaultFloat
{
    NSString* thevalue = [self validValue:thekey expectType:[NSString class]];
    if(nil == thevalue){
        NSNumber* thenum = [self validValue:thekey expectType:[NSNumber class]];
        if(nil == thenum){
            return defaultFloat;
        }
        return thenum.floatValue;
    }
    return thevalue.floatValue;
}

- (double) validDoubleValue:(id)thekey {
    return [self validDoubleValue:thekey withDefault:0.0];
}

- (double) validDoubleValue:(id)thekey withDefault:(double)defaultVal {
    NSString* thevalue = [self validValue:thekey expectType:[NSString class]];
    if(nil == thevalue){
        NSNumber* thenum = [self validValue:thekey expectType:[NSNumber class]];
        if(nil == thenum){
            return defaultVal;
        }
        return thenum.doubleValue;
    }
    return thevalue.doubleValue;
}

- (long) validLongValue:(id)thekey {
    
    NSNumber* thenum = [self validValue:thekey expectType:[NSNumber class]];
    if(thenum){
        return thenum.longValue;
    }
    
    NSString* thevalue = [self validValue:thekey expectType:[NSString class]];
    if(nil == thevalue){
        return 0;
    }
    return thevalue.longLongValue;
}

// 如果dic中没有有效值, 返回0
- (NSInteger) validIntegerValue:(id)thekey {
    return [self validIntegerValue:thekey withDefault:0];
}

// 如果dic中没有有效值, 返回0
- (NSInteger) validIntegerValue:(id)thekey withDefault:(NSInteger)defaultInt {
    NSString* thevalue = [self validValue:thekey expectType:[NSString class]];
    if(nil == thevalue){
        NSNumber* thenum = [self validValue:thekey expectType:[NSNumber class]];
        if(nil == thenum){
            return defaultInt;
        }
        return thenum.intValue;
    }
    return thevalue.intValue;
}


- (int64_t) validInt64Value:(id)thekey {
    return [self validInt64Value:thekey withDefault:0];
}
- (int64_t) validInt64Value:(id)thekey withDefault:(int64_t)defaultInt {
    NSString* thevalue = [self validValue:thekey expectType:[NSString class]];
    if(nil == thevalue){
        NSNumber* thenum = [self validValue:thekey expectType:[NSNumber class]];
        if(nil == thenum){
            return defaultInt;
        }
        return (int64_t)(thenum.longLongValue);
    }
    return (int64_t)(thevalue.longLongValue);
}



- (unsigned long long) validULLValue:(id)thekey {
    return [self validULLValue:thekey withDefault:0];
}
- (unsigned long long) validULLValue:(id)thekey withDefault:(unsigned long long)defaultInt {
    NSString* thevalue = [self validValue:thekey expectType:[NSString class]];
    if(nil == thevalue){
        NSNumber* thenum = [self validValue:thekey expectType:[NSNumber class]];
        if(nil == thenum){
            return defaultInt;
        }
        return thenum.unsignedLongLongValue;
    }
    return (unsigned long long)(thevalue.longLongValue);
}

- (NSDecimalNumber *) validNumber:(id)thekey withDefault:(NSDecimalNumber *)defaultNumber {
    NSDecimalNumber* thevalue = [self validValue:thekey expectType:[NSDecimalNumber class]];
    if (thevalue==nil) {
        NSString *theStrofvalue = [self validValue:thekey expectType:[NSString class]];
        if (theStrofvalue ) {
            thevalue = [NSDecimalNumber decimalNumberWithString:theStrofvalue];
        } else {
            thevalue = defaultNumber;
        }
    }
    return thevalue;
}

// 如果dic中没有有效值, 返回没有内容的NSArray对象
- (NSArray*) validArrayValue:(id)thekey {
    NSArray* thevalue = [self validValue:thekey expectType:[NSArray class]];
    if(nil == thevalue){
        return [NSArray array];
    }
    return thevalue;
}

// 如果dic中没有有效值, 返回没有内容的NSDictionary对象
- (NSDictionary*) validDicValue:(id)thekey {
    NSDictionary* thevalue = [self validValue:thekey expectType:[NSDictionary class]];
    if(nil == thevalue){
        return [NSDictionary dictionary];
    }
    return thevalue;
}

// 如果dic中没有有效值, 返回false
- (Boolean) validBoolValue:(id)thekey {

    NSNumber* thenum = [self validValue:thekey expectType:[NSNumber class]];
    if(thenum){
        return thenum.boolValue;
    }
    
	NSString *boolStr = [self validStringValue:thekey withDefault:@"false"];
	return [boolStr boolValue];
}

// 如果dic中没有有效值, 返回 当前时间对象
- (NSDate*) validDateValue:(id)thekey {
    NSDate* theDate = [self validValue:thekey expectType:[NSDate class]];
    return theDate ?: [NSDate date];
}

- (NSDate*) validDateValue:(id)thekey withDefault:(NSDate *)defaultDate {
    NSDate* theDate = [self validValue:thekey expectType:[NSDate class]];
    return theDate ?: defaultDate;
}

- (NSDictionary*) dicWithAppendKey:(id)theKey andValue:(id)theVal {
    if (!theKey || !theVal) {
        return [self copy];
    }
    
    NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithDictionary:self];
    mDic[theKey] = theVal;
    
    return [mDic copy];
}
- (NSDictionary*) dicWithAppendDic:(NSDictionary *)dic2Append {
    if (EmptyDicOrArray(dic2Append)) {
        return [self copy];
    }
    
    NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithDictionary:self];
    [mDic addEntriesFromDictionary:dic2Append];
    
    return [mDic copy];
}

- (NSDictionary*) changeDate2TimeStamp {
    NSMutableDictionary *rtnDic = [NSMutableDictionary dictionaryWithDictionary:self];
    for(id key in self) {
        if (rtnDic[key] && [rtnDic[key] isKindOfClass:NSDate.class]) {
            rtnDic[key] = @([((NSDate*)(rtnDic[key])) timeIntervalSince1970]);
        } else if (rtnDic[key] && [rtnDic[key] isKindOfClass:NSDictionary.class]) {
            NSDictionary *dicChanged = [((NSDictionary*)(rtnDic[key])) changeDate2TimeStamp];
            rtnDic[key] = dicChanged;
        }
    }
    
    return [rtnDic copy];
}
@end
